package com.ai.util;

import com.ai.common.CommonResponse;
import com.ai.entity.made.RegionBase;
import com.ai.entity.security.SysUser;
import com.ai.service.made.CameraService;
import com.ai.service.made.IRegionBaseService;
import com.ai.service.made.IRegionManageService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * 权限信息工具类
 * @Author jiaok
 * @Date 2023-07-24 16:41
 */
public class SecurityUtils {


    /**
     * 获取当前登录用户所管理的区域
     * @param iRegionManageService
     * @return
     */
    public static List<String> getRegionManageCode(IRegionBaseService iRegionManageService){
        Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();

        //获取所有的区域管理code,取交集
        List<RegionBase> regionBases = iRegionManageService.list();
        List<String> regionCodes = regionBases.stream().map(RegionBase::getRegionCode).collect(Collectors.toList());

        //判断当前用户是否为管理员


        ArrayList<String> manageRegionCodes = new ArrayList<>();
        for (String regionCode : regionCodes) {
            for (GrantedAuthority authority : authorities) {
                if (authority.getAuthority().toString().equals(regionCode)){
                    String code = authority.getAuthority().toString();
                    manageRegionCodes.add(code);
                }
            }
        }

        return manageRegionCodes;
    }

    /**
     * 获取当前登录的用户名
     * @return
     */
    public static String getLoginUserName(){
        SysUser sysUser = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = sysUser.getUserName();
        return name;
    }


    /**
     * 获取当前用户所属组织id
     * @return
     */
    public static String getLoginRegionBaseId(){
        SysUser sysUser = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return sysUser.getRegionBaseId();
    }

    /**
     * 获取当前登录的用户名
     * @return
     */
    public static String getLoginUserId(){
        SysUser sysUser = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return String.valueOf(sysUser.getId());
    }

    /**
     * 获取当前用户管理的区域内所有的摄像头code
     * @param iRegionBaseService 区域基本信息service
     * @param iRegionManageService 区域摄像头管理service
     * @return
     */
    public static HashMap<String,String> getRegionManageCamerasCode(IRegionBaseService iRegionBaseService,IRegionManageService iRegionManageService){
        //获取当前用针对的区域
        List<String> regionManageCodes = SecurityUtils.getRegionManageCode(iRegionBaseService);

//        AtomicReference<ArrayList<String>> cameraCodes = new AtomicReference<>(new ArrayList<>());
        AtomicReference<HashMap<String,String>> cameraRegionCodesMap = new AtomicReference<>(new HashMap<>());
//        ArrayList<RegionBase> baseArrayList = new ArrayList<>();

        regionManageCodes.forEach(regionManageCode ->{
            RegionBase regionBase = iRegionBaseService.getRegionBaseByCode(regionManageCode);
//            baseArrayList.add(regionBase);
            //获取当前区域所管理的所有摄像头
//            if (!CollectionUtils.isEmpty(baseArrayList)){
                CommonResponse bindList = iRegionManageService.getBindList(regionBase.getId().toString());
                ArrayList<String> bindListData = (ArrayList<String>) bindList.getData();
                for (String cameraCode : bindListData) {
                    cameraRegionCodesMap.get().put(cameraCode,regionManageCode);
                }
//                ArrayList<String> codes = cameraCodes.get();
//                codes.addAll();
//                cameraCodes.set(codes);
//              }
        });

        return cameraRegionCodesMap.get();
    }

    /**
     * 获取当前用户管理的区域内所有的摄像头code
     * @param iRegionBaseService
     * @param iRegionManageService
     * @param cameraService
     * @param regionCodes 查询条件中包含区域code
     * @return
     */
    public static List<String> getRegionManageCamerasCode(IRegionBaseService iRegionBaseService, IRegionManageService iRegionManageService, CameraService cameraService,List<String> regionCodes){
        //获取当前用针对的区域
        AtomicReference<ArrayList<String>> cameraCodes = new AtomicReference<>(new ArrayList<>());

        /**
         * 如果regionCodes 不为空，则说明是分页中的查询条件中，包含区域code
         */
        List<String> regionManageCodes = new LinkedList<>();
        if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(regionCodes)){
            regionManageCodes=regionCodes;
        }else {
           regionManageCodes= SecurityUtils.getRegionManageCode(iRegionBaseService);
        }

        if (CollectionUtils.isEmpty(regionManageCodes)){
            //todo 当前用户没有绑定区域，则默认为管理所有摄像头
            //如果是空的，则表明
//            List<CameraPO> camerasList = cameraService.list();
//            cameraCodes = (ArrayList<String>) camerasList.stream().map(CameraPO::getCode).collect(Collectors.toList());
            return cameraCodes.get();
        }else {
            regionManageCodes.forEach(regionManageCode ->{
                RegionBase regionBase = iRegionBaseService.getRegionBaseByCode(regionManageCode);
                //获取当前区域所管理的所有摄像头
                if (regionBase!=null){
                    CommonResponse bindList = iRegionManageService.getBindList(regionBase.getId().toString());
                    ArrayList<String> codes = cameraCodes.get();
                    codes.addAll((ArrayList<String>) bindList.getData());
                    cameraCodes.set(codes);
                }
            });
        }

        return cameraCodes.get();
    }
}
